package com.corner.system.service.impl;

import com.corner.common.core.domain.entity.SysUser;
import com.corner.common.exception.GlobalException;
import com.corner.common.utils.StringUtils;
import com.corner.system.domain.*;
import com.corner.system.domain.Approval;
import com.corner.system.domain.vo.SubsidyRequestVo;
import com.corner.system.domain.vo.transferQueryVo;
import com.corner.system.domain.vo.transferRepVo;
import com.corner.system.mapper.*;
import com.corner.system.service.SubsidyService;
import com.corner.system.service.WxPayService;
import com.corner.system.utils.OrderNoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 用户 业务层处理
 * 
 * @author corner
 */
@Service
public class SubsidyServiceImpl implements SubsidyService
{
    private static final Logger log = LoggerFactory.getLogger(SubsidyServiceImpl.class);

    @Autowired
    private SubsidyMapper subsidyMapper;
    @Autowired
    private IoTData_1Mapper ioTData1Mapper;
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SubsidyTempMapper subsidyTempMapper;
    @Autowired
    private ApprovalMapper approvalMapper;
    @Autowired
    private TransferBatchesMapper batchesMapper;



    @Transactional
    @Override
    public void getSubsidyOld(SubsidyRequestVo requestVo) {
        String deviceId = requestVo.getDeviceId();
        String batchNo = OrderNoUtils.getBatchNo();
        List<String> settlementMonths = requestVo.getSettlementMonths();

        //查询该月是否已申请或已经领取
        List<Subsidy> oldApplySubsidy = subsidyMapper.selectByDeviceIdAndBelongMonth(requestVo);
        if(oldApplySubsidy != null && oldApplySubsidy.size() > 0){
            for(Subsidy sb:oldApplySubsidy){
                //不是领取失败的状态
                if(!"2".equals(sb.getStatus())){
                    throw new GlobalException("您已申请领取"+ sb.getBelongMonth() +"补贴,无法重新领取");
                }
            }
        }

        //获取用户信息
        SysUser user = userMapper.selectUserByDeviceId(deviceId);
        PayToUserPurseRequest re = new PayToUserPurseRequest();
        re.setOut_batch_no(batchNo);
        re.setBatch_name("用户领取电费补贴");
        re.setBatch_remark("用户" + user.getUserName() + "进行领取电费补贴，userId:"+user.getUserId());
        re.setTotal_num(settlementMonths.size());
        
        List<Subsidy> subsidyList = new ArrayList<>();
        BigDecimal totalAmount = new BigDecimal("0");

        
        for(String settlementMonth: settlementMonths){
            //查询每月电费，并创建每月的补贴数据
            String[] split = settlementMonth.split("-");
            Map num3num5 = ioTData1Mapper.getDataByYearAndMonth(deviceId, split[0], split[1]);
            BigDecimal bigNum5 = (BigDecimal) num3num5.get("num5");//电费
            // 如果电费小于等于0
            if(!(bigNum5.compareTo(new BigDecimal(0)) == 1)){
                throw new GlobalException("该月电费为0元，无法领取补贴");
            }
            //测试
            bigNum5 = new BigDecimal("0.1");//1毛
            BigDecimal rounddown = bigNum5.setScale(2, BigDecimal.ROUND_DOWN);
            BigDecimal num5Fen = rounddown.multiply(new BigDecimal("100")).stripTrailingZeros();
            totalAmount = totalAmount.add(num5Fen);

            String detailNo = OrderNoUtils.getBatchDetailNo();
            //创建
            BatchDetails details = new BatchDetails();
            details.setOut_detail_no(detailNo);
            details.setOpenid(user.getOpenId());
            details.setUserId(user.getUserId());
            details.setTransfer_amount(num5Fen);
            details.setTransfer_remark("领取"+settlementMonth+"电费补贴");
            details.setDeviceId(deviceId);
            //details.setSubsidyId(subsidy.getId());
            List<BatchDetails> transfer_detail_list = re.getTransfer_detail_list();
            transfer_detail_list.add(details);
            re.setTransfer_detail_list(transfer_detail_list);

            Subsidy subsidy = new Subsidy();
            subsidy.setAmount(num5Fen);
            subsidy.setBelongMonth(settlementMonth);
            subsidy.setApplyTime(new Date());
            subsidy.setDeviceId(deviceId);
            subsidy.setCreateTime(new Date());
            subsidy.setStatus("0");
            subsidy.setDelFlag("0");
            subsidy.setTransferBatchNo(batchNo);
            subsidy.setTransferDetailNo(detailNo);
            subsidyList.add(subsidy);

            int i = subsidyMapper.insertSubsidy(subsidy);

        }
        re.setTotal_amount(totalAmount);
        //int i = subsidyMapper.insertSubsidyBatch(subsidyList);

        try {
            wxPayService.payToUserPurse(re);
        } catch (Exception e) {
            e.printStackTrace();
            for (Subsidy sb:subsidyList){
                sb.setStatus("2");
                sb.setRemark(e.getMessage());
                sb.setDelFlag("1");
            }
            //将插入的补贴数据状态修改为失败
            int i = subsidyMapper.updateSubsidyBatch(subsidyList);


        }


    }

    @Override
    public List<Subsidy> querySubsidyByDeviceId(String deviceId,String status)  throws Exception{
        List<Subsidy> subsidyList = subsidyMapper.querySubsidyByDeviceId(deviceId, status);
        return subsidyList;
    }

    /**
     * 查询管理员已审批可领取的补贴
     * @param subsidy
     * @return
     * @throws Exception
     */
    @Override
    public List<Subsidy> selectSubsidy(Subsidy subsidy)  throws Exception{
        String monthlimit = subsidyMapper.selectDatadesignValue("MONTHLIMIT");
        if(StringUtils.isBlank(monthlimit)){
            monthlimit = "3";//默认3个月
        }
        subsidy.setMonthLimit(Integer.parseInt(monthlimit));
        if(subsidy.getDeviceId()==null){
            throw new GlobalException("设备id不能为空");
        }
        subsidy.setApprovalFlag("1");//已审核通过的
        List<Subsidy> subsidyList = subsidyMapper.selectSubsidy(subsidy);
        /*for(Subsidy sb: subsidyList){
            BigDecimal amount = sb.getAmount();
            amount = amount.divide(new BigDecimal("100"),2,BigDecimal.ROUND_UNNECESSARY);
            sb.setAmount(amount);
        }*/
        return subsidyList;
    }




    @Override
    public Map querySuccessMoney(String deviceId) {
        Map map = subsidyMapper.querySuccessMoney(deviceId);
        return map;
    }





    /**
     * 批量审批补贴
     * @param subsidyTemps
     * @return
     */
    @Transactional
    @Override
    public String approvalBatch(List<SubsidyTemp> subsidyTemps,Long operator) {


        StringBuffer sb = new StringBuffer();
        String resultStr = null;
        List<Subsidy> subsidyList = new ArrayList<>();

        for(SubsidyTemp subsidyTemp: subsidyTemps){
            String belongYearAndMonth = subsidyTemp.getBelongMonth();
            String[] yearAndMonth = belongYearAndMonth.split("-");
            String deviceId = subsidyTemp.getDeviceId();
            String settlementYear = yearAndMonth[0];
            String settlementMonth = yearAndMonth[1];
            String electricity = subsidyTemp.getElectricity();//电量
            String amount = subsidyTemp.getAmount();//电费
            String flag = subsidyTemp.getApprovalFlag();
            if("0".equals(flag)){
                sb.append("设备"+deviceId+",补贴审核失败，原因：审核标识无效\n");
                continue;
            }
            if(StringUtils.isBlank(deviceId)){
                sb.append("此数据无设备id,补贴审核失败\n");
                continue;
            }
            if(StringUtils.isBlank(settlementYear)){
                sb.append("设备"+deviceId+",补贴审核失败，原因：补贴所属年份为空\n");
                continue;
            }
            if(StringUtils.isBlank(settlementMonth)){
                sb.append("设备"+deviceId+",补贴审核失败，原因：补贴所属月份为空\n");
                continue;
            }
            if(StringUtils.isBlank(electricity)){
                sb.append("设备"+deviceId+",补贴审核失败，原因：用电量为空\n");
                continue;
            }
            if(StringUtils.isBlank(amount)){
                sb.append("设备"+deviceId+",补贴审核失败，原因：电费为空\n");
                continue;
            }
            //查询该月是否已申请或已经领取
            Map<String,String> statusAndApprovalFlag = subsidyMapper.selectSubsidyStatusAndApprovalFlag(subsidyTemp);
            //String status = statusAndApprovalFlag.get("status");
            //该月补贴是否已审核通过 1、2已审核
            if(statusAndApprovalFlag != null  ){
                String approvalFlag = statusAndApprovalFlag.get("approvalFlag");
                if(!"0".equals(approvalFlag)){
                    sb.append("设备"+deviceId+"补贴审核失败,原因：已审核完成\n");
                    continue;
                }
                //throw new GlobalException("用户已申请领取"+ settlementMonth +"补贴");
            }

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, Integer.parseInt(settlementYear));
            cal.set(Calendar.MONTH,Integer.parseInt(settlementMonth)-1);
            cal.set(Calendar.DAY_OF_MONTH,1);

            //Map num3num5 = ioTData1Mapper.getDataByYearAndMonth(deviceId, split[0], split[1]);
            //BigDecimal bigNum5 = (BigDecimal) num3num5.get("num5");//电费
            BigDecimal bigNum5 = new BigDecimal(amount);
            // 如果电费小于等于0
            if(!(bigNum5.compareTo(new BigDecimal(0)) == 1)){
                throw new GlobalException("该月电费为0元，无法领取补贴");
            }

            BigDecimal rounddown = bigNum5.setScale(2, BigDecimal.ROUND_DOWN);
            BigDecimal num5Fen = rounddown.multiply(new BigDecimal("100")).stripTrailingZeros();

            Subsidy subsidy = new Subsidy();
            subsidy.setAmount(num5Fen);
            subsidy.setBelongMonth(settlementYear+"-"+settlementMonth);
            subsidy.setApplyTime(new Date());
            subsidy.setDeviceId(deviceId);
            subsidy.setCreateTime(new Date());
            subsidy.setDelFlag("0");
            subsidy.setApprovalFlag(subsidyTemp.getApprovalFlag());
            subsidy.setApprovalTime(new Date());
            subsidy.setElectricity(electricity);
            subsidy.setSubsidyDate(cal.getTime());
            //subsidy.setStatus("0");领取时再更新
            //subsidy.setTransferBatchNo(batchNo);
            //subsidy.setTransferDetailNo(detailNo);领取时再更新
            subsidyList.add(subsidy);
            int i = subsidyMapper.insertSubsidy(subsidy);
            sb.append("设备："+deviceId+"补贴审核通过！");

            subsidyTemp.setOperationAccount(operator);
            subsidyTemp.setStatus("1");

        }

        subsidyTempMapper.updateSubsidyTempBatch(subsidyTemps);

        resultStr = sb.toString();
        return resultStr;

    }



    /**
     * 全部审核
     * @param
     * @return
     */
    @Transactional
    @Override
    public String approvalAll(Long operationAccount,String operatorName,Long approvalId) {
        if(operationAccount == null){
            throw new GlobalException("登录人账号为空！");
        }
        if(operatorName == null){
            throw new GlobalException("操作人姓名为空！");
        }
        if(approvalId == null){
            throw new GlobalException("审批编号为空！");
        }
        SubsidyTemp st = new SubsidyTemp();
        st.setApprovalId(approvalId);
        List<SubsidyTemp> subsidyTemps = subsidyTempMapper.selectTempList(st);
        StringBuffer sb = new StringBuffer();
        String resultStr = null;
        List<Subsidy> subsidyList = new ArrayList<>();
        sb.append("审核完成！");
        for(SubsidyTemp subsidyTemp: subsidyTemps){
            String belongYearAndMonth = subsidyTemp.getBelongMonth();
            String[] yearAndMonth = belongYearAndMonth.split("-");
            String deviceId = subsidyTemp.getDeviceId();
            String settlementYear = yearAndMonth[0];
            String settlementMonth = yearAndMonth[1];
            String electricity = subsidyTemp.getElectricity();//电量
            String amount = subsidyTemp.getAmount();//电费
            String flag = subsidyTemp.getApprovalFlag();
            String status = subsidyTemp.getStatus();
            String farmerName = subsidyTemp.getFarmerName();
            String telephone = subsidyTemp.getTelephone();
            String address = subsidyTemp.getAddress();

            Boolean ifTemp = true;

            if(!ifTemp){
                if("1".equals(status)){
                    sb.append("农户："+farmerName+",设备"+deviceId+",提交审核失败，原因：已完成审核，无法重新审核\n");
                    continue;
                }
                if("0".equals(flag)){
                    String reason = "农户："+farmerName+",设备"+deviceId+",提交审核失败，原因：审核标识未变更\n";
                    sb.append(reason);
                    subsidyTemp.setApprovalFlag("2");
                    subsidyTemp.setFailReason(reason);
                    continue;
                }
                if(StringUtils.isBlank(deviceId)){
                    String reason = "农户："+farmerName+",此数据无设备id,补贴审核失败\n";
                    sb.append(reason);
                    subsidyTemp.setApprovalFlag("2");
                    subsidyTemp.setFailReason(reason);
                    continue;
                }
                if(StringUtils.isBlank(settlementYear)){
                    String reason = "农户："+farmerName+",设备"+deviceId+",提交审核失败，原因：补贴所属年份为空\n";
                    sb.append(reason);
                    subsidyTemp.setApprovalFlag("2");
                    subsidyTemp.setFailReason(reason);
                    continue;
                }
                if(StringUtils.isBlank(settlementMonth)){
                    String reason = "农户："+farmerName+",设备"+deviceId+",提交审核失败，原因：补贴所属月份为空\n";
                    sb.append(reason);
                    subsidyTemp.setApprovalFlag("2");
                    subsidyTemp.setFailReason(reason);
                    continue;
                }
                if(StringUtils.isBlank(electricity)){
                    String reason = "农户："+farmerName+",设备"+deviceId+",提交审核失败，原因：用电量为空\n";
                    sb.append(reason);
                    subsidyTemp.setApprovalFlag("2");
                    subsidyTemp.setFailReason(reason);
                    continue;
                }
                if(StringUtils.isBlank(amount)){
                    String reason = "农户："+farmerName+",设备"+deviceId+",提交审核失败，原因：电费为空\n";
                    sb.append(reason);
                    subsidyTemp.setApprovalFlag("2");
                    subsidyTemp.setFailReason(reason);
                    continue;
                }
            }

            //查询可能会导致整体速度变慢，暂时注掉
            /*//查询该月是否已申请或已经领取
            Map<String,String> statusAndApprovalFlag = subsidyMapper.selectSubsidyStatusAndApprovalFlag(subsidyTemp);
            //String status = statusAndApprovalFlag.get("status");
            //该月补贴是否已审核通过 1、2已审核
            if(statusAndApprovalFlag != null  ){
                String approvalFlag = statusAndApprovalFlag.get("approvalFlag");
                if(!"0".equals(approvalFlag)){
                    sb.append("设备"+deviceId+"补贴审核失败,原因：已审核完成\n");
                    continue;
                }
                //throw new GlobalException("用户已申请领取"+ settlementMonth +"补贴");
            }*/

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, Integer.parseInt(settlementYear));
            cal.set(Calendar.MONTH,Integer.parseInt(settlementMonth)-1);
            cal.set(Calendar.DAY_OF_MONTH,1);

            //Map num3num5 = ioTData1Mapper.getDataByYearAndMonth(deviceId, split[0], split[1]);
            //BigDecimal bigNum5 = (BigDecimal) num3num5.get("num5");//电费
            BigDecimal bigNum5 = new BigDecimal(amount);
            // 如果电费小于等于0
            /*if(!(bigNum5.compareTo(new BigDecimal(0)) == 1)){
                throw new GlobalException("该月电费为0元，无法领取补贴");
            }*/

            BigDecimal rounddown = bigNum5.setScale(2, BigDecimal.ROUND_DOWN);
            BigDecimal num5Fen = rounddown.multiply(new BigDecimal("100")).stripTrailingZeros();

            Subsidy subsidy = new Subsidy();
            subsidy.setApprovalId(approvalId);
            subsidy.setAmount(num5Fen);
            //12-12日要求，最多补贴不能超过十元
            BigDecimal limitPay = new BigDecimal("1000");
            if(num5Fen.compareTo(limitPay) == 1){
                subsidy.setPayAmount(limitPay);
            }else{
                subsidy.setPayAmount(num5Fen);
            }
            subsidy.setBelongMonth(settlementYear+"-"+settlementMonth);
            subsidy.setDeviceId(deviceId);
            subsidy.setCreateTime(new Date());
            subsidy.setDelFlag("0");
            subsidy.setApprovalFlag(subsidyTemp.getApprovalFlag());
            subsidy.setApprovalTime(new Date());
            subsidy.setElectricity(electricity);
            subsidy.setSubsidyDate(cal.getTime());
            subsidy.setApprovalUserId(operationAccount);
            subsidy.setFarmerName(farmerName);
            subsidy.setTelephone(telephone);
            subsidy.setAddress(address);
            //subsidy.setStatus("0");领取时再更新
            //subsidy.setTransferBatchNo(batchNo);
            //subsidy.setTransferDetailNo(detailNo);领取时再更新
            subsidyList.add(subsidy);
            subsidyMapper.insertSubsidy(subsidy);
            Long subsidyId = subsidy.getId();
            //sb.append("设备："+deviceId+"补贴审核完成！");

            subsidyTemp.setOperationAccount(operationAccount);
            subsidyTemp.setOperatorName(operatorName);
            subsidyTemp.setStatus("1");
            subsidyTemp.setSubsidyId(subsidyId);

        }

        int batchSize = 100; // 每批次插入的数据量

        if (subsidyTemps != null && subsidyTemps.size() > 0) {
            for (int i = 0; i < subsidyTemps.size(); i += batchSize) {
                int toIndex = Math.min(i + batchSize, subsidyTemps.size());
                List<SubsidyTemp> subList = subsidyTemps.subList(i, toIndex);
                subsidyTempMapper.updateSubsidyTempBatch(subList);
            }
        }

//        subsidyTempMapper.updateSubsidyTempBatch(subsidyTemps);

        Approval approval = approvalMapper.selectApprovalById(approvalId);
        String belongMonth = approval.getBelongMonth();
        Map statisticsData = subsidyTempMapper.getStatisticsData(belongMonth,approvalId);
        BigDecimal passSum = (BigDecimal) statisticsData.get("passSum");
        BigDecimal rejectSum = (BigDecimal) statisticsData.get("rejectSum");
        int passCount = (Integer) statisticsData.get("passCount");
        int rejectCount = (Integer) statisticsData.get("rejectCount");
        approval.setApprovedAmount(passSum);
        approval.setApprovedCounts(passCount);
        approval.setRejectCounts(rejectCount);
        approval.setApprovalDate(new Date());
        approval.setApprovalStatus("1");//已审批
        approval.setApprovalUserId(operationAccount);
        approval.setApprovalUserName(operatorName);
        approvalMapper.updateApproval(approval);

        resultStr = sb.toString();
        System.out.println("错误信息 =====>"+resultStr);

        if("" == resultStr || null == resultStr){
            throw new GlobalException(resultStr);
        }

        return resultStr;

    }

    public static void main(String[] args) {
        List result = new ArrayList();
        List subsidyTemps = new ArrayList();
        for(int j = 1; j < 100; j++){
            subsidyTemps.add(j);
        }

        int batchSize = 10; // 每批次插入的数据量

        if (subsidyTemps != null && subsidyTemps.size() > 0) {
            for (int i = 0; i < subsidyTemps.size(); i += batchSize) {
                int toIndex = Math.min(i + batchSize, subsidyTemps.size());
                List<SubsidyTemp> subList = subsidyTemps.subList(i, toIndex);
                result.addAll(subList);
//                subsidyTempMapper.updateSubsidyTempBatch(subList);
            }
        }
        System.out.println(result);
    }


    @Override
    public void changeApprovalFlag(SubsidyTemp subsidyTemp) {
        Long id = subsidyTemp.getId();
        String approvalFlag = subsidyTemp.getApprovalFlag();
        if(id == null){
            throw new GlobalException("id为空！");
        }
        if("0".equals(approvalFlag)){
            throw new GlobalException("审批标识不可为未审批！");
        }else{
            String status = subsidyTemp.getStatus();
            if("1".equals(status)){
                throw new GlobalException("提交审核后不可修改状态");
            }
        }

        int i = subsidyTempMapper.updateSubsidyTemp(subsidyTemp);
        if(i<=0){
            throw new GlobalException("修改状态失败！");
        }
    }


    @Transactional
    @Override
    public String getSubsidy(List<Subsidy> subsidyList) {
        StringBuffer sb = new StringBuffer();
        String resultStr = null;
        Subsidy subsidy1 = subsidyList.get(0);
        String deviceId = subsidy1.getDeviceId();
        String batchNo = OrderNoUtils.getBatchNo();

        //获取用户信息
        SysUser user = userMapper.selectUserByDeviceId(deviceId);


        PayToUserPurseRequest re = new PayToUserPurseRequest();
        re.setOut_batch_no(batchNo);
        re.setBatch_name("用户领取电费补贴");
        re.setBatch_remark(user.getUserName() + "领取电费");
        re.setTotal_num(subsidyList.size());

        BigDecimal totalAmount = new BigDecimal("0");

        for(Subsidy subsidy: subsidyList){
            String belongMonth = subsidy.getBelongMonth();
            //查询每月电费并核实
            /*String[] split = belongMonth.split("-");
            Map num3num5 = ioTData1Mapper.getDataByYearAndMonth(deviceId, split[0], split[1]);
            BigDecimal bigNum5 = (BigDecimal)num3num5.get("num5");//电费
            BigDecimal rounddown = bigNum5.setScale(2, BigDecimal.ROUND_DOWN);
            BigDecimal num5Fen = rounddown.multiply(new BigDecimal("100")).stripTrailingZeros();
            // 前端传递金额如果与查询电费金额不符
            if(num5Fen.compareTo(subsidy.getAmount()) != 0){
                throw new GlobalException(belongMonth+"电费金额有误，领取失败！");
            }*/
            //测试数据
            //BigDecimal num5Fen = new BigDecimal("10");//1毛

            //BigDecimal amount = subsidy.getAmount();
            //实际支付金额
            BigDecimal amount = subsidy.getPayAmount();

            //测试数据
            //amount = amount.divide(new BigDecimal("10"),0,BigDecimal.ROUND_DOWN);
            totalAmount = totalAmount.add(amount);


            String detailNo = OrderNoUtils.getBatchDetailNo();
            //创建
            BatchDetails details = new BatchDetails();
            details.setOut_detail_no(detailNo);
            details.setOpenid(user.getOpenId());
            details.setUserId(user.getUserId());
            details.setTransfer_amount(amount);
            details.setTransfer_remark("领取"+belongMonth+"电费补贴");
            details.setDeviceId(deviceId);
            details.setSubsidyId(subsidy.getId());//20240611加关联
            List<BatchDetails> transfer_detail_list = re.getTransfer_detail_list();
            transfer_detail_list.add(details);
            re.setTransfer_detail_list(transfer_detail_list);
            subsidy.setApplyTime(new Date());
            subsidy.setStatus("0");
            subsidy.setTransferBatchNo(batchNo);
            subsidy.setTransferDetailNo(detailNo);
            subsidy.setTelephone(user.getUserName());//由于可能在生成补贴后改变手机号，所以要更新领取时的登录手机号
            int i = subsidyMapper.updateSubsidy(subsidy);
        }
        re.setTotal_amount(totalAmount);

        try {
            wxPayService.payToUserPurse(re);

        } catch (Exception e) {
            e.printStackTrace();
            sb.append("领取失败");
            //throw new GlobalException("领取失败："+e.getMessage());
            for (Subsidy sub:subsidyList){
                sub.setStatus("2");
                sub.setRemark(e.getMessage());
                //sub.setDelFlag("1");
            }
            //将插入的补贴数据状态修改为失败
            subsidyMapper.updateSubsidyBatch(subsidyList);
            TransferBatches batches = new TransferBatches();
            batches.setBatchNo(batchNo);
            batches.setUpdateTime(new Date());//明细更新时间
            batches.setDetailStatus("FAIL");//明细状态
            batches.setCloseReason(e.getMessage());
            batches.setFailReason(e.getMessage());
            batchesMapper.updateTransferBatchesByBatchNoOrDetailNo(batches);


        }
        resultStr = sb.toString();
        return resultStr;
    }




    /**
     * 查询补贴列表
     * @param subsidy
     * @return
     * @throws Exception
     */
    @Override
    public List<Subsidy> selecSubsidyList(Subsidy subsidy) throws Exception{
        List<Subsidy> subsidyList = subsidyMapper.selectSubsidyList(subsidy);
        return subsidyList;
    }

    @Override
    public List<transferRepVo> selectTransferDetails(transferQueryVo query) throws Exception {
        List<transferRepVo> transferRep = subsidyMapper.selectTransferDetails(query);
        return transferRep;
    }


}
